var g_BtnLogin;
var g_InputPromptText;
var g_AccountEdit;
var g_PasswordEdit;
init();

function init(){
	g_BtnLogin = document.getElementById("ID_Btn_Login");
	g_AccountEdit = document.getElementById("ID_Account_Edit");
	g_InputPromptText = document.getElementById("ID_Input_Prompt");
	g_PasswordEdit = document.getElementById("ID_Code_Panel");
	g_InputPromptText.readOnly = true;
	registerEvent();
}

function registerEvent(){

	// clear Prompt output value when  account input get focus
	g_AccountEdit.onfocus = function(){
		g_InputPromptText.value = "";
	}

	g_PasswordEdit.onfocus = function(){
		g_InputPromptText.value = "";
	}

	// click on Button Login
	g_BtnLogin.onclick = function(){
		if ( isAccountLegal( g_AccountEdit.value,g_InputPromptText ) )
		{
			if ( isPassWordLegal(g_PasswordEdit.value,g_InputPromptText))
			{
					var sendMeg = prepareLoginReqCmd(g_AccountEdit.value);
					sendData(sendMeg);
			}
		}
		//location.href ="./WebQQ.html";
	}
}

function isPassWordLegal(passwordText,promptObj) 
{
	if ( passwordText == "" )
	{
		promptObj.value = "password is empty.";
		return false;
	}

	return true;
}

function isAccountLegal(accountText,promptObj) {
	if ( accountText == "" )
	{
		promptObj.value = "account is empty.";
		return false;
	}

	if ( isNaN(accountText) )
	{
		promptObj.value = "account need to be number";
		return false;
	}

	if ( accountText.length != 5)
	{
		promptObj.value = "the len of account number is 5.";
		return false;
	}

	if ( accountText[0] == '0')
	{
		promptObj.value = "the first number should not be 0";
		return false;
	}
	return true;
}

function prepareLoginReqCmd(accountText){
	return JSON.stringify(
		{
			cmd:"Client_Login_Req_Cmd",
			len:"1",
			account:accountText
		});
}

function sendData(msg){
	//新请求
	var request = new XMLHttpRequest();

	// 用post 向服务器发送脚本
	request.open("POST", "http://localhost:8081/");
	// 用请求主体发送纯文本消息
	request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
	request.send(msg);

	request.onreadystatechange = function() {
		//console.log("readyState:"+request.readyState )
		//console.log("request.status:"+request.status);
		if (request.readyState == 4 && request.status == 200) {
			var type = request.getAllResponseHeaders("Content-Type");
			//console.log(type);
			if (type.match("text")) {
				//callback(request.responseText);
				//console.log("server_res:"+request.responseText);
				//var jsonFormatRecvData = JSON.parse(request.responseText);
				parsePacket(JSON.parse(request.responseText));
				//console.log("recv_cmd:"+jsonFormatRecvData.cmd);
				//console.log("status:"+jsonFormatRecvData.status);
			}
		}
	}
}

function parsePacket(jsonPacket){
	switch(jsonPacket.cmd)
	{
		case "Server_Login_Res_Cmd":
			if (g_AccountEdit.value == jsonPacket.account)
			{
				if ( g_PasswordEdit.value == jsonPacket.password)
				{
					window.location = "./WebQQ.html"+"?account="+jsonPacket.account;
				}
				else
					updatePrompt("incorrect pwd");
			}	
			break;
	}
}

function updatePrompt(promptValue)
{
	g_InputPromptText.value = promptValue;
}
